package org.gangcai.mac.shop.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.just.agentweb.AgentWeb;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;

import org.greenrobot.eventbus.EventBus;
import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.ReadedBean;
import org.gangcai.mac.shop.common.MultiStatusActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BrowerActivity extends MultiStatusActivity {

    @BindView(R.id.activity_platform_introduce)
    LinearLayout activityPlatformIntroduce;
    @BindView(R.id.nextStep)
    Button nextStep;
    private CommonTitleBar titlebar;
    private LinearLayout root;
    private AgentWeb mAgentWeb;
    private boolean isShowButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        titlebar = (CommonTitleBar) findViewById(R.id.titlebar);
        multipleStatusView.showContent();
        root = (LinearLayout) findViewById(R.id.activity_platform_introduce);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String titlebar = extras.getString("titlebar");
            String url = extras.getString("url");
            isShowButton = extras.getBoolean("isShowButton");
            initTitleBarView(this.titlebar, titlebar);
            initWebView(url);
            if (isShowButton) {
                nextStep.setVisibility(View.VISIBLE);
            } else {
                nextStep.setVisibility(View.GONE);
            }
        }


    }

    private void initWebView(String url) {
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent((LinearLayout) root, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator(getResources().getColor(R.color.colorPrimary))
                .createAgentWeb()
                .ready()
                .go(url);
    }

    //点击返回上一页面而不是退出浏览器
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        mAgentWeb.getWebLifeCycle().onDestroy();
        super.onDestroy();
    }

    @Override
    public int getContentView() {
        return R.layout.activity_platform_introduce;
    }

    public static void openURL(String url, String title, boolean isShowButton) {
        Bundle options = new Bundle();
        options.putString("titlebar", title);
        options.putString("url", url);
        options.putBoolean("isShowButton", isShowButton);
        ActivityUtils.startActivity(options, ActivityUtils.getTopActivity(), BrowerActivity.class);
    }

    @OnClick(R.id.nextStep)
    public void onViewClicked() {
        if (isShowButton) {
            EventBus.getDefault().post(new ReadedBean());
        }
        finish();
    }
}
